package com.net.oil.car;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.car.adapter.MyCarRecyclerViewAdapter;
import com.net.oil.car.bean.CarBean;
import com.net.oil.car.contract.MyCarContract;
import com.net.oil.car.presenter.MyCarPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCarActivity extends BaseActivity<MyCarPresenter> implements MyCarContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.my_car_recycler_view)
    RecyclerView myCarRecyclerView;
    @BindView(R.id.add_car_button)
    Button addCarButton;

    private MyCarRecyclerViewAdapter adapter;
    private int carId = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_car);
        ButterKnife.bind(this);
        presenter = new MyCarPresenter(this);
        initView();
        initData();
    }

    private void initData() {

        carId = getIntent().getIntExtra(Constant.INTENT_SELECT_CAR_ID, -1);

        showProgressDialog();
        presenter.getMyCarList();
    }

    private void initView() {
        toolbarTitle.setText(getString(R.string.my_car));

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        adapter = new MyCarRecyclerViewAdapter(this, myCarRecyclerView);
        myCarRecyclerView.setLayoutManager(layoutManager);
        myCarRecyclerView.addItemDecoration(new DividerItemDecoration(this, layoutManager.getOrientation()));
        myCarRecyclerView.setAdapter(adapter);
    }

    @OnClick({R.id.toolbar_go_back, R.id.add_car_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
            case R.id.add_car_button:
                Intent intent = new Intent();
                intent.putExtra(Constant.INTENT_SELECT_CAR_ID, adapter.getSelectCarId());
                intent.putExtra(Constant.INTENT_SELECT_CAR_INFO, adapter.getSelectCarInfo());
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    /**
     * 获取爱车列表成功
     * @param carBeans  爱车列表
     */
    @Override
    public void getMyCarListSuccess(List<CarBean> carBeans) {
        dismissProgressDialog();
        if (carBeans.size() > 0){
            if (carId != -1){
                for (int i = 0; i < carBeans.size(); i++){
                    if (carBeans.get(i).getId() == carId){
                        adapter.setCheckPosition(i);
                    }
                }
            }
            adapter.updateData(carBeans);

        }else {
            startActivity(new Intent(this, MyCarDetailActivity.class));
        }
    }

    /**
     * 获取爱车列表失败
     */
    @Override
    public void getMyCarListFailed() {
        dismissProgressDialog();
    }
}
